import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    // 存储token
    Authorization: 'Bearer ' + localStorage.getItem('Authorization') ? localStorage.getItem('Authorization') : '',
    Cookies: localStorage.getItem('Cookies') ? localStorage.getItem('Cookies') : ''
  },

  mutations: {
    // 修改token，并将token存入localStorage
    changeLogin(state, user) {
      state.Authorization = 'Bearer ' + user.token;
      state.Cookies = 'userId=' + user.userId + '; ' + 'userName=' + user.userName + '; ' + 'token=' + user.token;
      localStorage.setItem('Authorization', state.Authorization);
      localStorage.setItem('Cookies', state.Cookies);
    }
  }
});

export default store;
